package com.ejlchina.tool.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import androidx.viewpager2.widget.ViewPager2
import com.adolf.androiddemo.R
import com.adolf.androiddemo.databinding.FragmentConfigBinding
import com.ejlchina.tool.MainActivity
import com.ejlchina.tool.adapter.pageadapter.ConfigFragmentOfflinePagerAdapter
import com.ejlchina.tool.base.BaseBindingFragment2
import com.ejlchina.tool.extension.safeNavigate
import com.ejlchina.tool.fragment.offline.ConfigOfflineViewPagerFragment
import com.ejlchina.tool.singleton.GlobalConnectObject
import com.ejlchina.tool.singleton.GlobalParams
import com.ejlchina.tool.util.LOG_TAG
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.huyuhui.fastble.data.BleDevice
import kotlinx.coroutines.launch


class ConfigOfflineFragment : BaseBindingFragment2<FragmentConfigBinding>() {
    private val fragmentList = mutableListOf<ConfigOfflineViewPagerFragment>()
    private var pageAdapter :ConfigFragmentOfflinePagerAdapter? = null
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
        initData()
    }

    private fun initViews(){
        Log.i(LOG_TAG,"Config fragment onViewCreated")
        setHeadVisibility(View.GONE)
//        setFirstComeLogic()
        initListeners()
    }

    private fun initData(){
        initTabAndViewPager()
    }

    private fun initListeners(){
        binding.includeHead.ivMenu.setOnClickListener {
            (requireActivity() as MainActivity).openDraw()
        }
//        binding.layoutNoDevice.btnOpenBluetooth.setOnClickListener {
//            findNavController().navigate(R.id.action_config_to_scan)
//        }

        viewLifecycleOwner.lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.RESUMED){
                setConnectCount()
            }
        }

        binding.includeHead.tvConnectCount.setOnClickListener {
            findNavController().safeNavigate(R.id.action_config_to_scan)
        }
    }

    private fun initTabAndViewPager(){
        binding.tabLayout.tabMode = TabLayout.MODE_SCROLLABLE
        fragmentList.clear()
        fragmentList.addAll(GlobalConnectObject.getCurrentConnectDeviceList().map { ConfigOfflineViewPagerFragment(it) })
        binding.viewPager.offscreenPageLimit = ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT
        Log.i(LOG_TAG,"Fragment Page Size---${fragmentList.size}")
        pageAdapter = ConfigFragmentOfflinePagerAdapter(this,fragmentList)
        binding.viewPager.adapter = pageAdapter
        TabLayoutMediator(
            binding.tabLayout, binding.viewPager
        ) { tab, position -> tab.setText(GlobalParams.getBucketIndexFormBleDevice(fragmentList[position].bleDevice.bleDevice)) }.attach()
    }

    private fun setConnectCount(){
        if (isAdded){
            binding.includeHead.tvConnectCount.text = "${viewModelActivity.getAllConnectDevicesCount()}"
        }
    }

    override fun whenDeviceDisConnect(disConnectDevice: BleDevice) {
        super.whenDeviceDisConnect(disConnectDevice)
        dismissLoading()
        if (isAdded){
            setConnectCount()
//            setFirstComeLogic()
            if (viewModelActivity.getAllConnectDevicesCount() > 0){
                initTabAndViewPager()
            }
        }
    }
    override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup): FragmentConfigBinding {
        return FragmentConfigBinding.inflate(inflater,container,true)
    }

    override fun initOnCreateView() {
        super.initOnCreateView()
        setActionBarPadding(false)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        GlobalParams.isStartDebugMode = false
        Log.i(LOG_TAG,"Config fragment onDestroy View")
    }

}
